# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

import datetime

#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

if request.env.web2py_runtime_gae:            # if running on Google App Engine
    db = DAL('google:datastore')              # connect to Google BigTable
                                              # optional DAL('gae://namespace')
    session.connect(request, response, db = db) # and store sessions and tickets there
    ### or use the following lines to store sessions in Memcache
    # from gluon.contrib.memdb import MEMDB
    # from google.appengine.api.memcache import Client
    # session.connect(request, response, db = MEMDB(Client()))
else:                                         # else use a normal relational database
    db = DAL('sqlite://storage.sqlite')       # if not, use SQLite or other DB

# by default give a view/generic.extension to all actions from localhost
# none otherwise. a pattern can be 'controller/function.extension'
response.generic_patterns = ['*'] if request.is_local else []

#########################################################################
## Here is sample code if you need for
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################

from gluon.tools import Mail, Auth, Crud, Service, PluginManager, prettydate
mail = Mail()                                  # mailer
auth = Auth(db)                                # authentication/authorization
crud = Crud(db)                                # for CRUD helpers using auth
service = Service()                            # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()                      # for configuring plugins

mail.settings.server = 'logging' or 'smtp.gmail.com:587'  # your SMTP server
mail.settings.sender = 'you@gmail.com'         # your email
mail.settings.login = 'username:password'      # your credentials or None

auth.settings.hmac_key = 'sha512:0d77d0f2-af47-4cfe-970b-c60160dc4ca5'   # before define_tables()
auth.define_tables()                           # creates all needed tables
auth.settings.mailer = mail                    # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'

#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled = \
#    ['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################

crud.settings.auth = None        # =auth to enforce authorization on crud

#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################


db.define_table( 'filteredContribution', Field( 'firstName', 'string' ),
                                         Field( 'age', 'string' ),
                                         Field( 'hometown', 'string' ),
                                         Field( 'contribution', 'text' ),
                                         Field( 'question', 'string' ),
                                         Field( 'datetime', 'datetime', default=datetime.datetime.now() ) )


db.define_table( 'kioskContribution', Field( 'firstName', 'string' ),
                                      Field( 'age', 'string' ),
                                      Field( 'hometown', 'string' ),
                                      Field( 'contribution', 'text' ),
                                      Field( 'question', 'string' ),
                                      Field( 'datetime', 'datetime', default=datetime.datetime.now() ),
                                      Field( 'processed', 'boolean', default=False ),
                                      Field( 'approved', 'boolean', default=False ) )


if( db( db.kioskContribution.id > 0 ).count() == 0 ):

    db.kioskContribution.insert( firstName = 'Charles', age = '18', hometown = 'Oak Park, IL', contribution = 'The events of 9/11 did not change how I define a hero. I was too young to have a concrete definition of what a hero was, I think.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Kate', age = '', hometown = 'Chicago, IL', contribution = 'The word "hero" remained the same for me.  I feel that anyone, person or animal - the young or old, put into a situation where they would need to help someone or something else out, would do it. People helped other people. People helped animals. Animals helped people.  A hero is a hero.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )

    db.kioskContribution.insert( firstName = 'Francesca Moroni', age = '19', hometown = 'Chicago, IL', contribution = 'I think that 9/11 proved that a true hero is anyone who is willing to put others above their own fears. 9/11 also proved that crying wasn\'t a sign of weakness, but a sign of strength and love.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Krystal', age = '30', hometown = 'Chicago, IL', contribution = 'I don’t think that the word hero was re-defined for me by 9/11. I’ve always thought of firefighters and policemen as heroes and I think what 9/11 accomplished was to highlight the depth of their heroism in the face of an incredibly heinous act.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Stella', age = '30', hometown = 'Chicago, IL', contribution = 'To be honest, no. I always thought those firefighters et al were heroic because I couldn\'t imagine putting myself at such risk like that everyday as my job. I was impressed, however, how normal citizens immediately rallied to do what they could. People housing people they barely knew because they couldn\'t get home since the trains and bridges leaving Manhattan were closed. The rush of people at the hospital who tried to donate blood; so many in fact, that most of us were turned away because the hospital couldn\'t cope. So I guess, in a way, when I think about it, that\'s pretty heroic too, even if it\'s to a lesser degree.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )

    db.kioskContribution.insert( firstName = 'Alaka', age = '', hometown = 'Chicago, IL', contribution = 'Not really', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Janet', age = '60', hometown = 'Chicago, IL', contribution = 'Sure. Those folks on the plane that crashed in the middle of Pennsylvania instead of hitting the White House—just imagine how they must have worked together to prevent further disaster. I wonder what I myself would have done if I had been on that plane, or in the World Trade Center. Would I have panicked? Or helped others? I don’t need other people to see me as a hero if ever there were a crisis, but I sure as hell hope I can keep calm and help others.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )

    db.kioskContribution.insert( firstName = 'Nicole', age = '22', hometown = 'Orfordville, WI', contribution = 'No. The term "hero" has always had a wide range in my life--from fictional superheroes, to mygrandfather who fought in WWII, to my mother who protects me to this day with her love and wisdom. Public safety personnel and "ordinary" citizens can both be heroes, as proven by 9/11.', question = 'A', datetime = datetime.datetime(2011,8,23,12,00), approved = True )

    db.kioskContribution.insert( firstName = 'Alaka', age = '', hometown = 'Chicago, IL', contribution = 'The missing person flyers', question = 'B', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Janet', age = '60', hometown = 'Chicago, IL', contribution = 'I’m not Christian— I don’t have any religion at all— but when I saw those crosses made out of World Trade Center metal, I was surprised by a spontaneous phrase that escaped my lips: “PleasePLEASE have mercy on all us poor sinners.” I don’t know if that was a prayer or what. I’m glad the iron workers found something worth while to create out of the metal. Memorials for innocent people, who were just going to work on a Tuesday morning.', question = 'B', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Nicole', age = '22', hometown = 'Orfordville, WI', contribution = 'While many of the images and objects are moving, two particular objects strike me more. The bolt:the size is memorable and makes me even more awed that towers built of such material could indeed fall. The fire helmet: the object alone is interesting, then adding this event and loss of human life touches me even more.', question = 'B', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Ellen', age = '33', hometown = 'Los Angeles, CA', contribution = 'To me it was interesting to see a piece of steel from the World Trade Center. Everyone remembers seeing tv footage of the towers collapsing, over and over. But seeing the actual steel makes the whole thing seem real and impossible at the same time.', question = 'B', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Charles', age = '18', hometown = 'Oak Park, IL', contribution = 'What really scared me about the events of 9/11 was the changes to society that followed it. The increase in security bothered me, even as a child. I distinctly remember hearing from my sister that the Statue of Liberty was closed and feeling very frightened by the notion that such an iconic place wasn\'t open any more. The events themselves didn\'t scare me; the way the government reacted to them did.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Francesca Moroni', age = '19', hometown = 'Chicago, IL', contribution = 'I was afraid that Chicago, my school, my house, would be attacked.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Lindsey', age = '24', hometown = 'Plainfield, IL', contribution = 'I think what scared me most in the aftermath of 9/11 was a sense of loss. I could no longer trust in the goodness of humanity.  This sadness reached me one day while I was at The Field Museum.  I remember volunteering out in Stanley Field Hall shortly after 9/11, and there was a camera bag left on one of the benches.  Just as I was thinking about heading over to pick it up and turn it in to lost and found, I saw a security guard go over to the bench. But the guard didn\'t pick up the bag.  He had to radio for another guard to come with a test kit to make sure this bag was not a bomb or otherwise dangerous.  This scared me - not because I thought this bag might really be a bomb, but because I saw this as a reflection on humanity.  What kind of world is it where everyone and everything must be handled warily because you never know who or what could bring terrible destruction?', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Anne Marie', age = '27', hometown = 'North Canton, OH', contribution = 'When I first heard that a plane had hit the WTC, I thought it was an accident. I will never forget standing in the cafeteria of my high school, watching the live news coverage of the situation and seeing the second plane hit the other tower. At that moment I realized the planes crashes were not accidents.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Krystal', age = '30', hometown = 'Chicago, IL', contribution = 'I was most scared about the fact that it was an orchestrated event. I remember watching the news that morning and seeing the coverage of the planes hitting the first tower and then the second. At first, I had no thoughts of terrorism or war, only that an air traffic controller or a pilot had made a really bad mistake. When it dawned on me that this was a planned attack, that was when the fear hit.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Kate', age = '', hometown = 'Chicago, IL', contribution = 'What scared me the most was to really realize that no one is safe from terrorism.  It can happen anywhere at any time.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Stella', age = '30', hometown = 'Chicago, IL', contribution = 'This is a very atypical answer but it has made me somewhat claustrophobic sometimes. The funny thing is I didn\'t even realize it until nearly 4 years later when I was in a subway between Queens and Manhattan on the way to work. The train just stopped underground and the announcer said nothing. I hadn\'t felt that uncomfortable since the attacks when it took me over a month to get on the subway and go downtown. I just couldn\'t bear the thought of people potentially being stuck underground in a train on the way to the World Trade Center and maybe it took a while before they could escape. I\'m not even sure if this actually happened or not, but there it is. 9/11 has also made me more of a suspicious person, which scares me that I could be like that. But I sometimes if I see something now,I will think, what\'s that in his bag? Why did that teenager just leave that box on the ground like that? But that\'s life now.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Alaka', age = '', hometown = 'Chicago, IL', contribution = 'The dread of not knowing where there would be more attacks and the dread of the power it gave to the US government to act (like go to war).', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Janet', age = '60', hometown = 'Chicago, IL', contribution = 'On September 11, I remember being afraid to turn on my kitchen faucet. I actually thought the water might be poisoned. I know that sounds crazy, but that day we all thought that the United States was completely under attack, by unknown forces, who wanted us all dead. Yet in the next few days, I was moved by how much support for the U.S came in from all around the world. Many people didn’t wish us dead, but indeed thought the U.S. was a place of hopes and dreams, and good people. ARE we a place of hopes and dreams, and good people?', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
    
    db.kioskContribution.insert( firstName = 'Nicole', age = '22', hometown = 'Orfordville, WI', contribution = 'The attack on the World Trade Center towers introduced me to the word "terrorist". As a 7thgrader, I didn\'t realize the dangers that we (the human race) are all constantly subjected to. It opened my eyes with worry.', question = 'C', datetime = datetime.datetime(2011,8,23,12,00), approved = True )
